//
//  Game.m
//  2048
//
//  Created by 高新川 on 15/6/7.
//  Copyright (c) 2015年 高新川. All rights reserved.
//

#import "Game.h"

@implementation Game

static Game *defaultGame = nil;

#pragma mark - 初始化
- (id)init {
    self = [super init];
    if (self) {
        //_matrix = 4;            // 默认最简单的游戏
        _model = [[DataModel alloc] init]; // 创建数据模型
        
        // 创建算法
        _algorithm = [[Algorithm alloc] init];
    }
    return self;
}

#pragma mark - 四个方向的操作
- (BOOL)top {
    if (_algorithm) {
        [_algorithm caculateTop:_model];
        _addScore = [_algorithm getAddScore];
        return [_algorithm randNewOne:_model];
    }
    return YES;
}

- (BOOL)bottom {
    if (_algorithm) {
        [_algorithm caculateBottom:_model];
        _addScore = [_algorithm getAddScore];
    }
    return [_algorithm randNewOne:_model];
}

- (BOOL)left {
    if (_algorithm) {
        [_algorithm caculateLeft:_model];
        _addScore = [_algorithm getAddScore];
    }
    return [_algorithm randNewOne:_model];
}

- (BOOL)right {
    if (_algorithm) {
        [_algorithm caculateRight:_model];
        _addScore = [_algorithm getAddScore];
    }
    return [_algorithm randNewOne:_model];
}

#pragma mark - 游戏控制
- (void)initGame {
    [_model initNumbers];
}

#pragma mark - 游戏单例
+ (Game *)getInstance {
    if (defaultGame == nil) {
        defaultGame = [[Game alloc] init];
    }
    return defaultGame;
}

@end
